package MtG.client.panels.dialogs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import MtG.publicClass.Player;
import MtG.publicClass.decks.FireDeck;
import MtG.publicClass.decks.ForestDeck;

/**
 * Class that is used as a template for user input dialog box.
 * @author Marek Sirp
 * @author Jaan Sepp
 */
public class NewPlayer extends JDialog{
	
	/**
	 * Determines if a de-serialized file is compatible with this class.
	 */
	private static final long serialVersionUID = 2954436636414821L;
	/**
	 * Label for asking the players name.
	 */
	private JLabel askName = new JLabel("What is your name ?");
	/**
	 * Label for asking the user to choose a deck to play.
	 */
	private JLabel askDeck = new JLabel(" Choose your preferable deck");
	/**
	 * Box object that displays its components in a column.
	 */
	private Box b = Box.createVerticalBox();
	/**
	 * Field for letting the user enter his name.
	 */
	private JTextField answer = new JTextField();
	/**
	 * Choice button for the firedeck.
	 */
	private JRadioButton firedeck = new JRadioButton("Firedeck");
	/**
	 * Choice button for the forest deck.
	 */
	private JRadioButton forestdeck = new JRadioButton("Forestdeck");
	/**
	 * Field for holding reference to the player using this client.
	 */
	private Player p;
    /**
     * Group for gathering the radio buttons into one selection.
     */
    private ButtonGroup group = new ButtonGroup();
    /**
     * Creating the "ok" button object.
     */
    private JButton ok = new JButton("Ok");
    /**
     * Creating the panel object.
     */
    private JPanel p2 = new JPanel();
	
	/**
	 * Constructor
	 * @param parent parent frame of this frame.
	 * @param p player that uses the current client.
	 */
	public NewPlayer(JFrame parent, Player p){   	
		super(parent, "Creating player", true);
		this.p = p;
		firedeck.setSelected(true);
		forestdeck.setActionCommand("forest");
		firedeck.setActionCommand("fire");
        setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        
	    getContentPane().add(b);
	    b.add(askName);
	    b.add(answer);
	    b.add(askDeck);
	    b.add(firedeck);
	    b.add(forestdeck);

	    p2.add(ok);
	    b.add(p2);

	    group.add(firedeck);
	    group.add(forestdeck);

	    ok.addActionListener(new Listener());
	    
	    pack();
	    setMinimumSize(getSize()); 
	    setMaximumSize(getSize());
	    setVisible(true);   	  
	}
    /**
     * Class that is added as a listener template for the user dialog box
     * that is shown when a clientConnection is launched.
     * @author Marek Sirp
     * @author Jaan Sepp
     */
    public class Listener implements ActionListener{
    	public void actionPerformed(ActionEvent e){
	      p.setClientName(answer.getText());
    	  if((group.getSelection().getActionCommand()).equals("forest")){
    		  p.setDeck(new ForestDeck());
    	  } else if ((group.getSelection().getActionCommand()).equals("fire")){
    		  p.setDeck(new FireDeck());
    	  }
	        setVisible(false);
      }
    }
    
    /**
     * @return reference to the player using the current client.
     */
    public Player getPlayer(){
    	return p;
    }
}